package aim;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Vector;

/**
 * Options.java
 * 
 * This classes parses the command line arguments passed to the program and
 * sets up run time variables appropriately. In particular, Options sets up
 * custom break characters, input files, and the stop word file.
 * 
 * Modified and reused from Project 2 Team 1 (WMI) code
 * @author Andrew Bernard, Zachary M. Allen
 */
public class Options {

	/** Input file object vector */
	private Vector inputs;

	/** Stop word file object */
	private File stopWordFile;

	/** Break characters array */
	private char[] breakChars;

	/** True if stop words are set, false otherwise */
	private boolean stopWordsSet;

	/** True if break characters are set, false otherwise */
	private boolean breakCharsSet;

	/**
	 * Constructor
	 * @param    args    Command line arguments to be processed
	 */
	public Options(String[] args) {		
		inputs = null;
		stopWordFile = null;
		breakChars = " :/.,+*()~&%$#!`\"{}|^[]\\@?<>=;".toCharArray();
		stopWordsSet = false;
		breakCharsSet = false;
		
		try {
			parseOptions(args);
		} catch (FileNotFoundException ex) {
			Constants.usageError("File not found");
		} catch (IOException ex) {
			Constants.usageError("Error reading from file");
		}
	}

	/**
	 * This method checks all the arguments passed into the program. It will set
	 * the appropriate input stream according to the specified arguments or it
	 * will throw an error if there is a problem with the arguments.
	 * 
	 * @param args
	 *            list of arguments passed into the program
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public void parseOptions(String[] args) throws FileNotFoundException,
			IOException {
		
		int firstFileIndex = 0;
		File file = null;
		inputs = new Vector();
		
		if (isOption(args[0]) && isOption(args[2])) {
			firstFileIndex = 4;
		} else if (isOption(args[0])) {
			firstFileIndex = 2;
		} else {
			firstFileIndex = 0;
		}
		
		for (int i = 0; i < firstFileIndex; i += 2) {
			setOption(args[i], args[i + 1]);
		}
		
		for (int j = firstFileIndex; j < args.length; j++) {
			file = new File(args[j]);
			validateFile(file);
			inputs.add(new InputFile(args[j]));
		}
		
	}

	/**
	 * Determines if a parameter is an option
	 * @param    string    Parameter
	 * @return    True if parameter is an option, false otherwise
	 */
	private boolean isOption(String string) {
		boolean retval = false;
		if (string.equals("-b") || string.equals("-s")) {
			retval = true;
		}
		return retval;
	}

	/**
	 * Sets an option to a particular value
	 * @param    option    Option to set
	 * @param    setting    Value to set it to
	 */
	private void setOption(String option, String setting) {
		if (option.equals("-b") && !breakCharsSet) {
			breakChars = setting.toCharArray();
			breakCharsSet = true;
		} else if (option.equals("-s") && !stopWordsSet) {
			stopWordFile = new File(setting);
			validateFile(stopWordFile);
			stopWordsSet = true;

		} else {
			Constants.usageError();
		}
	}

	/**
	 * Checks if <code>file</code> exists and is readable; an error will be
	 * thrown if either status is false.
	 * 
	 * @param file
	 *            the file to validate
	 */
	private void validateFile(File file) {
		if (!file.exists() || !file.canRead()) {
			Constants.usageError();
		}
	}
  
  public char[] getBreakChars() {
    return breakChars;
  }
  
  public Vector getInputs() {
    return inputs;
  }


  public File getStopWordFile() {
    return stopWordFile;
  }


}